package com.six.firstpage.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.six.core.entity.Goods;
import com.six.core.entity.ZoneGoods;
import com.six.core.vo.GoodsVo;
import com.six.core.vo.Result;
import com.six.firstpage.feign.GoodsFeign;
import com.six.firstpage.service.ZoneGoodsService;
import com.six.firstpage.mapper.ZoneGoodsMapper;
import com.six.firstpage.vo.GoodsAddVo;
import com.six.firstpage.vo.ZoneGoodsVo;
import org.apache.commons.codec.net.QCodec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* @author ZZH
* @description 针对表【tb_zone_goods(专区商品关联表)】的数据库操作Service实现
* @createDate 2023-03-02 11:16:18
*/
@Service
public class ZoneGoodsServiceImpl extends ServiceImpl<ZoneGoodsMapper, ZoneGoods>
    implements ZoneGoodsService{

    @Autowired
    private ZoneGoodsMapper zoneGoodsMapper;

    @Autowired
    private GoodsFeign goodsFeign;


    public Result getZoneGoods(Integer current, Integer pageSize, GoodsVo goodsVo){

        //查询所有的商品信息

        PageInfo pageInfo = goodsFeign.listpaging(current, pageSize, goodsVo);

        List<Goods> list = pageInfo.getList();

        //查询已经上架的专区商品信息
        QueryWrapper<ZoneGoods> zoneGoodsQueryWrapper = new QueryWrapper<>();
        zoneGoodsQueryWrapper.eq("is_deleted",0);
        List<ZoneGoods> records = zoneGoodsMapper.selectList(zoneGoodsQueryWrapper);


        ArrayList<GoodsAddVo> goodsAddVos = new ArrayList<>();

        //比较专区商品的信息和商品信息,确定该商品是否已经在专区内
        list.stream().forEach(
                goods -> {
                    GoodsAddVo goodsAddVo = new GoodsAddVo(goods,0);
                    records.stream().forEach(record->{
                        if (record.getGoodsid().equals(goods.getId())){
                            goodsAddVo.setAdd(1);
                        }
                    });
                    goodsAddVos.add(goodsAddVo);
                }
        );

        pageInfo.setList(goodsAddVos);

        return  new Result(2000,"success",pageInfo);
    }



    @Override
    public IPage<ZoneGoodsVo> getZoneGoodsById(ZoneGoodsVo vo , Integer current, Integer pageSize) {

        IPage<ZoneGoodsVo> page = new Page<>(current,pageSize);

        zoneGoodsMapper.getZoneGoods(vo,page);

        return page;
    }

    @Override
    public Integer addZonGoods(ZoneGoods zoneGoods) {

        return zoneGoodsMapper.insert(zoneGoods);
    }

    @Override
    public Integer updateZoneGoods(ZoneGoods zoneGoods) {
        return zoneGoodsMapper.updateById(zoneGoods);
    }

    @Override
    public Integer delZoneGoods(Integer id) {
        UpdateWrapper<ZoneGoods> zoneGoodsUpdateWrapper = new UpdateWrapper<>();
        zoneGoodsUpdateWrapper.eq("id",id);
        zoneGoodsUpdateWrapper.set("is_deleted",1);

        return zoneGoodsMapper.update(null,zoneGoodsUpdateWrapper);
    }
}




